package com.gherasoft.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.gherasoft.model.CheckListModel;
import com.gherasoft.view.R;

import java.util.ArrayList;
import java.util.List;

/**
 * User: sckomoroh
 * Date: 3/13/13
 * Time: 8:35 AM
 */
public class MainAdapter extends BaseAdapter
{
    private List<CheckListModel> model = new ArrayList<CheckListModel>();
    private Context context;

    public MainAdapter(Context context)
    {
        this.context = context;
    }

    public void clear()
    {
        model.clear();
        notifyDataSetChanged();
    }

    public void addItem(CheckListModel item)
    {
        model.add(item);
        notifyDataSetChanged();
    }

    public void removeItem(int index)
    {
        model.remove(index);
        notifyDataSetChanged();
    }

    public void update()
    {
        notifyDataSetChanged();
    }

    @Override
    public int getCount()
    {
        return model.size();
    }

    @Override
    public Object getItem(int index)
    {
        return model.get(index);
    }

    public int getIndex(Object item)
    {
        return model.indexOf(item);
    }

    @Override
    public long getItemId(int i)
    {
        return i;
    }

    @Override
    public View getView(int index, View view, ViewGroup viewGroup)
    {
        CheckListModel item = model.get(index);

        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.main_item, null);

        TextView textView = (TextView) view.findViewById(R.id.mainItemTextView);
        TextView completeTextView = (TextView) view.findViewById(R.id.itemCompleteTextView);

        textView.setText(item.getListName());

        int completed = 0;
        for (int i=0; i<item.getCount(); i++)
        {
            if (item.isItemChecked(i))
            {
                completed++;
            }
        }

        completeTextView.setText(String.format("%s / %s", completed, item.getCount()));

        return view;
    }
}
